Imports System.Net.Mail

Namespace Network
    ''' <summary>
    ''' Class used for sending mails trough an SMTP server
    ''' </summary>
    ''' <remarks>An SMTP server is a requirement for sending mails </remarks>
    Public Class Email
        Private _myMessage As New MailMessage
        Private _LinkedResources As LinkedResourceCollection

        ''' <summary>
        ''' Gets or sets the encoding type that will be applyied to the mail body. 
        ''' The default character set is "us-ascii"
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public Property BodyEncoding() As System.Text.Encoding
            Get
                Return _myMessage.BodyEncoding
            End Get
            Set(ByVal value As System.Text.Encoding)
                _myMessage.BodyEncoding = value
            End Set
        End Property
        ''' <summary>
        ''' Sends an E-mail using the last given parameters  such as SMTP server, headers, Body encoding, etc.
        ''' <example><code></code>
        ''' </example>
        ''' </summary>
        ''' <param name="SMTPServer">SMTP Server Name</param>
        ''' <param name="To">A semicolon-separated string containing each recipient's email account</param>
        ''' <param name="Subject">The mail subject</param>
        ''' <param name="Body">The mail body</param>
        ''' <param name="Sender">The mail sender name</param>
        ''' <param name="CC">A semicolon-separated string containing a list of addresses that should receive a copy of the mail</param>
        ''' <param name="BCC">A semicolon-separated string containing a list of addresses that should receive a blind carbon copy of the mail</param>
        ''' <param name="IsHTML">A boolean value specifying whether the mail's body is in an HTML format</param>
        ''' <param name="SMTPUser">Email account used to send the mail</param>
        ''' <param name="SMTPPass">Password of the person who sends de mail</param>
        ''' <returns>If is True, the mail was sent successfully, and in case of False it wasn't sent</returns>
        ''' <remarks>After calling this function, all the properties get cleared, so make sure before calling this funcion, 
        ''' all the necessary properties are filled</remarks>
        Public Function Send(ByVal SMTPServer As String, ByVal [To] As String, _
        ByVal Subject As String, ByVal Body As String, Optional ByVal Sender As String = "", _
                Optional ByVal CC As String = "", Optional ByVal SMTPUser As String = "", _
                Optional ByVal SMTPPass As String = "", Optional ByVal IsHTML As Boolean = False, _
                Optional ByVal BCC As String = "") As Boolean
            'Dim myMessage As New MailMessage

            Dim SMTPSvr As New SmtpClient(SMTPServer)
            Dim From As MailAddress
            Dim Mails() As String
            Dim MailsCC() As String
            'Dim MailCC As String
            Dim MailsBCC() As String
            'Dim MailBCC As String
            Dim view As AlternateView

            'Dim Mail As String
            Try
                Mails = [To].Split(";")
                MailsCC = CC.Split(";")
                MailsBCC = BCC.Split(";")
                If SMTPUser <> "" And SMTPPass <> "" Then
                    SMTPSvr.Credentials = New Net.NetworkCredential(SMTPUser, SMTPPass)
                End If
                With _myMessage
                    If Sender <> "" Then
                        Dim SenderMail As New MailAddress(Sender)
                        .Sender = SenderMail
                        .From = SenderMail
                    Else
                        From = New MailAddress(Mails(0))
                        .From = From
                    End If
                    If CC <> "" Then
                        Dim CCMail As New MailAddress(MailsCC(0))
                        .ReplyTo = CCMail
                        For Each MailCC As String In MailsCC
                            .CC.Add(MailCC)
                        Next
                    End If
                    If BCC <> "" Then
                        Dim BCCMail As New MailAddress(MailsBCC(0))
                        .ReplyTo = BCCMail
                        For Each MailBCC As String In MailsBCC
                            .Bcc.Add(MailBCC)
                        Next
                    End If
                    For Each Mail As String In Mails
                        .To.Add(Mail)
                    Next
                    .Subject = Subject
                    .IsBodyHtml = IsHTML
                    .Body = Body
                End With

                If Not IsNothing(_LinkedResources) Then
                    If _LinkedResources.Count > 0 Then
                        view = AlternateView.CreateAlternateViewFromString(Body)
                        For Each lr As LinkedResource In _LinkedResources
                            view.LinkedResources.Add(lr)
                        Next
                        _myMessage.AlternateViews.Add(view)
                    End If
                End If

                SMTPSvr.Send(_myMessage)

                _myMessage.AlternateViews.Clear()
                _myMessage.Attachments.Clear()

                SMTPSvr = Nothing
                Mails = Nothing
                MailsCC = Nothing
                MailsBCC = Nothing
                view = Nothing

                Return True
            Catch ex As Exception
                Throw New Exception(ex.Message, ex)
                Return False
            End Try
        End Function
        ''' <summary>
        ''' <example><code></code>
        ''' </example>
        ''' </summary>
        ''' <param name="FileName">Absolute path to the file to add as an attachment</param>
        ''' <param name="MediaType">A string containing the type of media that is going to be sent. 
        ''' This value can be obtained from any of the classes contained in the System.Net.Mime class</param>
        Public Sub AddAttachment(ByVal FileName As String, Optional ByVal MediaType As String = System.Net.Mime.MediaTypeNames.Application.Octet)
            _myMessage.Attachments.Add(New Attachment(FileName, MediaType))
        End Sub

        ''' <summary>
        ''' Function that embeds a resource (usually an image) to use it within an html tag
        ''' <example>
        ''' Within the img tag in the html document: '...img src=cid:header... '
        ''' Within the application: EmailInstance.AddLinkedResource("c:\myheader.JPG","header")
        ''' </example>
        ''' </summary>
        ''' <param name="ResourcePath">Absolute path to the file to add as Linked Resource</param>
        ''' <param name="cid">A string containing the ID to be used within the html body</param>
        ''' <param name="ContentType">The content-type of the linked resource, which is "image/jpeg" by default</param>
        Public Sub AddLinkedResource(ByVal ResourcePath As String, ByVal cid As String, Optional ByVal ContentType As String = "image/jpeg")
            Dim lr As New LinkedResource(ResourcePath)
            lr.ContentId = cid
            lr.ContentType.MediaType = ContentType
            If _LinkedResources.IndexOf(lr) < 0 Then
                _LinkedResources.Add(lr)
            End If
            lr.Dispose()
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="name"></param>
        ''' <param name="value"></param>
        ''' <remarks></remarks>
        Public Sub AddHeader(ByVal name As String, ByVal value As String)
            _myMessage.Headers.Add(name, value)
        End Sub

        ''' <summary>
        ''' Sets the priority that the Mail will be sent with
        ''' </summary>
        ''' <param name="Priority"></param>
        ''' <remarks></remarks>
        Public Sub SetPriority(ByVal Priority As System.Net.Mail.MailPriority)
            _myMessage.Priority = Priority
        End Sub

        ''' <summary>
        ''' Specify when a delivery notification will be sent back
        ''' </summary>
        ''' <param name="Options"></param>
        ''' <remarks></remarks>
        Public Sub SetDeliveryNotificationOptions(ByVal Options As System.Net.Mail.DeliveryNotificationOptions)
            _myMessage.DeliveryNotificationOptions = Options
        End Sub
    End Class
End Namespace


